﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Custom;
using Business;
using Entity;
using FreshCms.BackEnd.ControlsCommon;
using Newtonsoft.Json;
using Utilities;

namespace FreshCms.BackEnd.Controls.Systems.Group
{
    public partial class PmsManager : System.Web.UI.UserControl
    {
        public Int32 groupid = 0;
        public string lstShortPms = string.Empty;

        #region check login and permission
        InPms chkPms = new InPms();
        public Int32 panelId = 0;
        private void checkLogin()
        {
            // Check and load UC
            if (Request.QueryString["ui"] != null)
                Int32.TryParse(Request.QueryString["ui"], out panelId);
            // check Pms
            if (panelId > 0)
                chkPms = InPmsBusiness.CheckSessionPms(panelId);
            if (chkPms == null)
                Response.Redirect("/BackEnd/xlogin.aspx?redirect=" + Utils.GetCurrentCmsPage().Replace("?", "@").Replace("&", "|"));
            // show Pms in Top Bar
            TopBarPms ucTopBar = Page.Master.FindControl("TopBarPms") as TopBarPms;
            ucTopBar.BindData(chkPms);
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            Int32.TryParse(Request.QueryString["groupid"], out groupid);
            if ((!Page.IsPostBack) && (groupid>0))
            {
                if (chkPms.PmsView == 1)
                {
                    BindListUser();
                    BindCate();
                }
            }
        }

        #region List User

        private void BindListUser()
        {
            var lst = InUserBusiness.GetInUserListByGroupId(groupid);
            //var lst = InUserBusiness.GetInUserList();
            
            if (lst.Count > 0)
            {
                rptListUser.DataSource = lst;
                rptListUser.ItemDataBound += new RepeaterItemEventHandler(rptListUser_ItemDataBound);
                rptListUser.DataBind();
            }
        }

        void rptListUser_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    InUser obj = (InUser)e.Item.DataItem;

                    lblStatus.Text = StatusBusiness.GetShortStatusName(obj.UserStatus);

                    if (obj.UserStatus == 1)
                    {
                        lblStatus.CssClass = "label label-success";
                    }
                    else
                    {
                        lblStatus.CssClass = "label label-danger";
                    }
                }
                catch (Exception ex) { }
            }
        }
        #endregion

        #region Category

        private void BindCate()
        {
            var lst = InPmsBusiness.GetInPanelListByGroupId(groupid);

            if (lst.Count > 0)
            {
                rpt.DataSource = lst;
                rpt.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
                rpt.DataBind();

                //var result = rows.Where(kv => colnames.Contains(kv.Key)).Select(kv => kv.Value);
                var result = from p in lst select new {p.PmsId, p.GroupId, p.PanelId, p.PmsView, p.PmsUpdate, p.PmsDelete, p.PanelSatus};
                lstShortPms = JsonConvert.SerializeObject(result);
            }
        }

        void rpt_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                try
                {
                    //Label lblStatus = (Label)e.Item.FindControl("lblStatus");
                    PlaceHolder phParent = (PlaceHolder)e.Item.FindControl("phParent");
                    PlaceHolder phChild = (PlaceHolder)e.Item.FindControl("phChild");

                    InPms obj = (InPms)e.Item.DataItem;

                    if(obj.PanelParentId==0)
                    {
                        phParent.Visible = true;
                    }else
                    {
                        phChild.Visible = true;
                    }
                    
                }
                catch (Exception ex) { }
            }
        }

        #endregion

    }

    
}